<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="120px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="项目编号:" prop="xmBh">
            <el-input
              v-model="queryParams.xmBh"
              placeholder="请输入项目编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目名称:" prop="xmName">
            <el-input
              v-model="queryParams.xmName"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申请部门:" prop="sqBm">
            <el-input
              v-model="queryParams.sqBm"
              placeholder="请输入申请部门"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="支出选项:" prop="zcXx">
            <el-select v-model="queryParams.zcXx" placeholder="请选择支出选项" clearable>
              <el-option
                v-for="dict in dict.type.lstb_zcxx"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="项目实施状态:" prop="xmSsZt">
            <el-select v-model="queryParams.xmSsZt" placeholder="请选择项目实施状态" clearable>
              <el-option
                v-for="dict in dict.type.lstb_xmsszt"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="研发成果证书号:" prop="yfCgZsh">
            <el-input
              v-model="queryParams.yfCgZsh"
              placeholder="请输入研发成果证书号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fztz:lstb:add']"
        >增加课题
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fztz:lstb:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fztz:lstb:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fztz:lstb:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              class="fixedTableTwo"  height="300" v-loading="loading" :data="lstbList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column show-overflow-tooltip label="项目名称" align="center" prop="xmName">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.xmName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目编号" align="center" prop="xmBh"/>
      <el-table-column show-overflow-tooltip label="项目截止时间" align="center" prop="zTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.zTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目延期" align="center" prop="yqCount">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleXmYqCk(scope.row)"
          >
            <el-link v-if="scope.row.yqCount != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.yqCount == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="申请部门" align="center" prop="sqBm"/>
      <el-table-column show-overflow-tooltip label="支出选项" align="center" prop="zcXx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lstb_zcxx" :value="scope.row.zcXx"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目实施状态" align="center" prop="xmSsZt">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lstb_xmsszt" :value="scope.row.xmSsZt"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="项目类型" align="center" prop="xmLx">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lstb_xmlx" :value="scope.row.xmLx"/>
        </template>
      </el-table-column>
      <el-table-column label="立项信息" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleLxXxClick(scope.row)"
          >查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="研发活动记录" align="center" prop="clxhtbId">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleClXhCk(scope.row)"
          >
            <el-link v-if="scope.row.clxhtbId != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.clxhtbId == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="合同变更" align="center" prop="htBgCount">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleHtBgCk(scope.row)"
          >
            <el-link v-if="scope.row.htBgCount != null" type="primary">{{ '查看' }}</el-link>
          </el-button>
          <el-link type="danger" style="color: #0D1623" disabled v-if="scope.row.htBgCount == null">{{ '无' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fztz:lstb:edit']"
          >编辑课题
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="vxe-icon-edit"
            @click="handleAddXmLs(scope.row)"
          >流水填报
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流水填报对话框 -->
    <vxe-modal :title="title" v-model="open" width="50%" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="110px">
        <div style="height: 40vh;">
          <!-- 课题信息 -->
          <el-card class="box-card">
            <div slot="header" class="clearfix" style="text-align: center;">
              <span style="font-size: 20px; color: #1890ff">课题信息</span>
            </div>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="项目编号" prop="xmBh" @focus="selectKt">
                  <el-input v-model="form.xmBh" placeholder="请输入项目编号" @focus="selectKt()"
                            :suffix-icon="'el-icon-search'"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目名称" prop="xmName">
                  <el-input readonly v-model="form.xmName" placeholder="请输入项目名称"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item v-if="1 === 2" label="项目申报ID" prop="xmsbId">
              <el-input v-model="form.xmsbId" placeholder="请输入项目申报ID"/>
            </el-form-item>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="申请部门" prop="sqBm">
                  <el-input readonly v-model="form.sqBm" placeholder="请输入申请部门"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="支出选项" prop="zcXx">
                  <el-select v-model="form.zcXx" placeholder="请选择支出选项">
                    <el-option
                      v-for="dict in dict.type.lstb_zcxx"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="项目实施状态" prop="xmSsZt">
                  <el-select v-model="form.xmSsZt" placeholder="请选择项目实施状态">
                    <el-option
                      v-for="dict in dict.type.lstb_xmsszt"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="研发成果" prop="yfCg">
                  <el-input v-model="form.yfCg" placeholder="请输入研发成果"/>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="24">
              <el-col :span="12">
                <el-form-item label="研发成果证书号" prop="yfCgZsh">
                  <el-input v-model="form.yfCgZsh" placeholder="请输入研发成果证书号"/>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="项目类型" prop="xmLx">
                  <el-select v-model="form.xmLx" placeholder="请选择项目类型">
                    <el-option
                      v-for="dict in dict.type.lstb_xmlx"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-card>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>

    <!-- 科研项目选择 -->
    <HtXzKt ref="ktXz"/>

    <!-- 查询项目申报(主表 + 子表) 弹窗 -->
    <Xmxq ref="xmxq"/>

    <!-- 项目流水 -->
    <Xmls ref="xmlS"/>

    <!-- 合同变更查看 -->
    <HtBgCk ref="htBgCk"/>

    <!-- 项目延期查看 -->
    <XmYqCk ref="xmYqCk"/>

    <!-- 流水填报查看 -->
    <LsTbXq ref="lsTbXq"/>

    <!-- 材料消耗查看页面 (研发活动记录查看) -->
    <ClXhCk ref="clXhCk"/>
  </div>
</template>

<script>
import {addLstb, delLstb, getLstb, getLsTbId, listLstb, updateLstb} from "@/api/fztz/lstb";
import HtXzKt from "@/views/components/ktXz/HtXzKt.vue";
import Xmxq from "@/views/xmgl/lxgl/xmsb/components/xmxq.vue";
import Xmls from "@/views/fztz/xmls/index.vue";
import HtBgCk from "@/views/xmgl/htgl/htbz/components/HtBgCk.vue";
import XmYqCk from "@/views/xmgl/htgl/htbz/components/XmYqCk.vue";
import LsTbXq from "@/views/fztz/lstb/components/LsTbXq.vue";
import ClXhCk from "@/views/fztz/components/clxhck/ClXhCk.vue";
import {selectYbFyList} from "@/api/fztz/xmls";

export default {
  name: "Lstb",
  components: {ClXhCk, LsTbXq, XmYqCk, HtBgCk, Xmls, Xmxq, HtXzKt},
  dicts: ['lstb_xmsszt', 'lstb_zcxx', 'lstb_xmlx'],
  provide() {
    return {
      updateXmsbId: (val) => {
        this.form.xmBh = val.ktBh
        this.$refs['formDialog'].validateField('xmBh');
        this.form.xmName = val.ktName
        this.$refs['formDialog'].validateField('xmName');
        this.form.xmsbId = val.xmsbId
        this.form.sqBm = val.zrBm
      }
    }
  },
  data() {
    return {
      getLsTbId: null, //从后端获取到的流水填报ID
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流水填报表格数据
      lstbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xmBh: null,
        xmName: null,
        xmsbId: null,
        sqBm: null,
        zcXx: null,
        xmSsZt: null,
        yfCgZsh: null,
        xmLx: null,
        idWhere: null,
        userId: this.$store.state.user.id,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xmBh: [{required: true, message: "项目编号不能为空", trigger: "blur"}],
        xmName: [{required: true, message: "项目名称不能为空", trigger: "blur"}],
        zcXx: [{required: true, message: "支出选项不能为空", trigger: "change"}],
        xmSsZt: [{required: true, message: "项目实施状态不能为空", trigger: "change"}],
        xmLx: [{required: true, message: "项目类型不能为空", trigger: "change"}],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //材料消耗查看页面 (研发活动记录查看)
    handleClXhCk(row) {
      this.$refs.clXhCk.handleOpen(row)
    },
    //打开流水填报详情
    handleButtonClick(row) {
      this.$refs.lsTbXq.handleOpen(row)
    },
    //点击项目延期打开项目延期查看弹窗
    handleXmYqCk(row) {
      this.$refs.xmYqCk.handleOpen(row);
    },
    //点击变更次数打开合同变更查看弹窗
    handleHtBgCk(row) {
      this.$refs.htBgCk.handleOpen(row);
    },
    //打开项目流水弹窗页面
    handleAddXmLs(row) {
      this.$refs.xmlS.handleOpen(row)
    },
    //打开立项信息详情
    handleLxXxClick(row) {
      this.$refs.xmxq.handleOpen(row);
    },
    // 打开选择科研项目弹窗
    selectKt() {
      this.$refs.ktXz.handleOpen();
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询流水填报列表 */
    getList() {
      this.loading = true;
      listLstb(this.queryParams).then(response => {
        this.lstbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        lstbId: this.getLsTbId,
        xmBh: null,
        xmName: null,
        xmsbId: null,
        sqBm: null,
        zcXx: null,
        xmSsZt: null,
        yfCg: null,
        yfCgZsh: null,
        xmLx: '自主研发',
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      };
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.lstbId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      getLsTbId().then(res => {
        this.getLsTbId = res.msg
        this.reset();
      })
      this.open = true;
      this.title = "增加课题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const lstbId = row.lstbId || this.ids
      getLstb(lstbId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流水填报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          if (this.form.idWhere != null) {
            updateLstb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLstb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const lstbIds = row.lstbId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delLstb(lstbIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fztz/lstb/export', {
        ...this.queryParams
      }, `lstb_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
